define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');
	var dspUtil = require('dspUtil');

	var Word = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'url': 'campaign/searchWordAndIds'
			});

			//以_id 为索引的数据集合.
			this.$data = {};

			//选中记录的id号
			this.$ids = [];
			this.Super('init', arguments);
		}
		,afterBuild: function() {
			var self = this;
			var c = self.getConfig();
			self.append($([
				'<div class="P-campaignEditBodyVideoDirectCon">'
					,'<div class="total">'
						,'<label>'+ LANG('搜索结果:') +'</label>'
						,'<div pub-name="total" pub-mod="@base/grid.baseNoDate"></div>'
					,'</div>'
					,'<div class="select">'
						,'<label>'+ LANG('已选结果:') +'</label>'
						,'<div pub-name="select" pub-mod="@base/table.list" class="selectCon"></div>'
					,'</div>'
				,'</div>'
				,'<div class="cl"></div>'
			].join('')));


			var config = {
				'total' : {
					'cols': [
						{type: 'id'},
						{name: 'Keyword', text:LANG('关键词'), sort: false, 'align': 'left'},
						{name: 'Bid', text:LANG('昨日竞价量'), sort: false, 'align': 'center'}
					],
					'sub_field': 'Id',
					'url': c.url,
					'reqType': 'websocket',
					'param': {
						'type': 'videoDirectWord'
					},
					'hasSelect': true,
					'hasTab': false,
					'hasAmount': false,
					'hasExport': false,
					'pager':{
						'size': 20,
						'bounds': 5,
						'firstLast': false,
						'showJumper': 0,
						'showSizeTypes': 0
					},
					'list':{
						'rowSelect': true,
						'scroll_type': 'row',
						'scroll_size': 10
					},
					'auto_load': false,
					'eventDataLoad': true
				}
				,'select': {
					"cols":[
						{type:"id"},
						{name: 'Keyword', text:LANG('关键词'), sort: false, 'align': 'left'},
						{name: 'Bid', text:LANG('昨日竞价量'), sort: false, 'align': 'center'},
						{type:"op", html:'<a data-op="remove" title="'+LANG("删除此条记录")+'">'+LANG("删除")+'</a>'}
					],
					"opClick": true,
					"scroll_type": "row",
					"scroll_size": 10,
					"key":"Id"
				}
			};

			self.buildFromTemplate(config);
		}
		,afterBuildTemplate: function() {
		}
		,reset: function() {
			this.$data = {};
			this.$ids = [];
			this.$.total.setData([]);
			this.$.select.setData([]);
			return this;
		}
		,load: function(ids) {
			if(ids && ids.length){
				this.$ids = ids;
				this.$.select.showLoading();
				pubjs.mc.send(this.getConfig('url'), {
					'type': 'ids',
					'ids': ids
				}, this.onData.bind(this));
			}
			return this;
		}
		,onData: function(err, data) {
			this.$.select.hideLoading();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			this.setTotalData(data.items || []);
		}
		,onGridDataLoad: function(ev){
			var item = ev.param && ev.param.items;
			this.setTotalData(item || []);
		}
		,getValue: function(){
			return this.$ids;
		}
		,setValue: function(data){
			this.$ids = data;
			//右边表格setData
			this.showResultList();
			return this;
		}
		,setTotalData: function(data) {
			var self = this;
			self.$data = self.$data || {};
			util.each(data, function(item){
				self.$data[item.Id] = item;
			});
			// 显示选中的记录
			self.showResultList();
		}
		,showResultList: function(){
			var all = this.$data;
			var data = this.$ids;
			if (all){
				var list = [];
				var ids = [];
				util.each(data, function(id){
					if (all[id]){
						list.push(all[id]);
						ids.push(id);
					}
				});
				this.$ids = ids;
				this.$.total.setSelectRowIds(ids);
				this.$.select.setData(list);
			}
		}
		,onListOpClick:function(ev){
			//删除单条记录
			var index = ev.param.index;
			this.$.select.removeRow(index);

			//更新this.$ids的值
			var listData = this.$.select.getData();
			this.$.select.setData(listData);
			var ids = this.$ids = [];
			util.each(listData, function(size){
				ids.push(size.Id);
			});

			//更新表格的勾选状态
			this.$.total.setSelectRowIds(ids);
			ids = null;
			return false;
		}
		,onChangeSelect:function(ev){
			//总数据
			var data = this.$data;
			var ids = this.$ids = ev.param.selected;
			var arr = [];

			util.each(ids, function(id){
				if (data[id]){
					arr.push(data[id]);
				}
			});

			this.$.select.setData(arr);

			ids = null;
			return false;
		}
		,getDesc: function(){
			var self = this;
			var data = self.$data;
			var desc = [];
			if(self.$data){
				util.each(self.$ids, function(item){
					if(data[item]){
						desc.push(data[item].Keyword);
					}
				});
			}
			return desc;
		}
	});

	exports.word = Word;

	var Type = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'readonly': false,
				'url': 'campaign/videoDirectChannel'
			});

			this.Super('init', arguments);
			this.$ids = [];
		}
		,afterBuild: function() {
			var self = this;
			//var c = self.getConfig();
			self.append($([
				'<div class="P-campaignEditBodyOrientVideoTyep">',
					'<div pub-name="type" pub-mod="@base/common/input.subLevelCheckbox"/>',
				'</div>'
			].join('')));

			var config = {
				'type': {
					'key': 'SubClass',
					"valueKey":"ClassId",
					"nameKey":"ClassName",
					"silence": false
				}
			};

			self.buildFromTemplate(config);
		}
		,afterBuildTemplate: function() {
			this.load();
		}
		,load: function(){
			var self = this;
			var el = self.getDOM();
			var url = self.getConfig('url');
			if(!util.checkUI(el, 'load', self)){
				return self;
			}
			pubjs.mc.send(url, function(err, data){
				if(err){
					pubjs.alert(err.message);
					return false;
				}
				var tmpData = {
					'ClassId': 0,
					'ClassName': LANG('全部'),
					'SubClass': data
				}
				data = [tmpData];
				self.$.type.setList(data);
				self.fire('dataLoadEnd');
			});
			return self;
		}
		,getData: function(){
			var tmpIds = this.$ids;
			dspUtil.map(tmpIds || [], Number);
			return tmpIds;
		}
		,setData: function(ids){
			ids = ids || [];
			this.$ids = ids;
			this.$.type.setData(ids);
		}
		,reset: function() {
			this.$ids = [];
			this.$.type.reset();
			return this;
		}
		,onSubLevelChange: function(ev){
			var data = ev.param;
			this.$ids = data.data;
		}
		,getDesc: function(){
			return this.$.type.getDesc();
		}
	});
	exports.type = Type;

});
